<!--
 * @Description: 
 * @Author: ldx
 * @Date: 2023-03-02 21:05:41
 * @LastEditors: ldx
 * @LastEditTime: 2023-03-02 21:27:10
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body{
      margin: 0;
      overflow: hidden;
    }
    #canvas{
      /* background: red; */
    }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <script>
    const canvas = document.querySelector('#canvas')
    // 设置canvas容器的宽高
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight
    // 获取webgl上下文
    const gl = canvas.getContext('webgl');
    // 声明将来用来清空绘图区的颜色。分别对应r,g,b,a
    gl.clearColor(200,0,142,0.5)
    // 使用之前声明的颜色，清空绘图区
    gl.clear(gl.COLOR_BUFFER_BIT)

    const reg = new RegExp(/\((.*)\)/)
    // 颜色转化的工具函数
    function colorToRgba(color){
      const str = reg.exec(color)[1]
      const rgba = str.split(',').map(parseInt)
      const r = rgba[0] / 255
      const g = rgba[1] / 255
      const b = rgba[2] / 255
      const a = rgba[3] 
      return [r,g,b,a]
    }
    const color = 'rgba(255,0,0,1)'
    const [r,g,b,a] = colorToRgba(color)
    gl.clearColor(r,g,b,a)
    gl.clear(gl.COLOR_BUFFER_BIT)
  </script>
</body>
</html>